package com.cloudspace.yunloan.ui.refresh;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.cloudspace.yunloan.R;


public abstract class RefreshAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    /**
     * item 类型
     */
    public final static int TYPE_HEADER = 0;//头部--支持头部增加一个headerView
    public final static int TYPE_FOOTER = 1;//底部--往往是loading_more
    public final static int TYPE_START = 2;//普通--用户自定义

    private boolean mIsHeaderEnable;

    /**
     * 标记当前是否支持footer,数据加载完成后footerEnable变为false
     */
    private boolean footerEnable;

    /**
     * 标记是否正在加载更多，防止再次调用加载更多接口
     */
    private boolean mIsLoadingMore;

    protected Context mContext;

    public RefreshAdapter(Context context) {
        mContext = context;
        footerEnable = false;
        mIsLoadingMore = false;
        mIsHeaderEnable = false;
    }

    @Override
    public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // 默认header和footer
        RecyclerView.ViewHolder viewHolder = onRefreshCreateViewHolder(parent, viewType);
        if (viewHolder == null) {
            switch (viewType) {
                case RefreshAdapter.TYPE_FOOTER: {
                    viewHolder = new FooterViewHolder(LayoutInflater.from(parent.getContext()).inflate(
                            R.layout.refresh_loading, parent, false));
                }
                break;
            }
        }
        return viewHolder;
    }


    @Override
    public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (isFooterPosition(position)) {
            return;
        }

        onRefreshBindViewHolder(holder, position);
    }

    @Override
    public final int getItemCount() {
        int count = getRefreshItemCount();

        if (count > 0) {
            if (isFooterEnable()) count++;
        }
        if (isHeaderEnable()) count++;

        return count;
    }

    /**
     * 此处只负责header和item两项
     */
    @Override
    public final int getItemViewType(int position) {
        if (isHeaderPosition(position)) {
            return TYPE_HEADER;
        }
        if (isFooterPosition(position)) {
            return TYPE_FOOTER;
        }

        // 给用户索引数据的下标,去掉header的位置.
        int tempPosition = position;
        if (isHeaderEnable()) tempPosition--;

        return getRefreshItemViewType(tempPosition);
    }

    public abstract RecyclerView.ViewHolder onRefreshCreateViewHolder(ViewGroup parent, int viewType);

    public abstract void onRefreshBindViewHolder(RecyclerView.ViewHolder holder, int position);

    public abstract int getRefreshItemCount();

    public int getRefreshItemViewType(int position) {
        return TYPE_START;
    }

    /**
     * 是否允许加载更多
     */
    public boolean isFooterEnable() {
        return footerEnable;
    }

    /**
     * 设置是否支持自动加载更多,数据加载完成后,为false
     *
     * @param autoLoadMore
     */
    public void setFoolterEanble(boolean autoLoadMore) {
        footerEnable = autoLoadMore;
    }

    /**
     * 设置是否正在加载中
     */
    public void setLoadingMore(boolean isLoadingMore) {
        mIsLoadingMore = isLoadingMore;
    }

    public boolean isLoadingMore() {
        return mIsLoadingMore;
    }

    /**
     * 判断当前位置是否是foolter
     */
    public boolean isFooterPosition(int position) {
        int footerPosition = getItemCount() - 1;
        return isFooterEnable() && position == footerPosition;
    }

    /**
     * 是否是header坐标
     */
    public boolean isHeaderPosition(int position) {
        return isHeaderEnable() && position == 0;
    }

    /**
     * 获取item选项的位置坐标
     */
    public int getItemPosition(int position) {
        if (isHeaderEnable()) {
            return position - 1;
        } else {
            return position;
        }
    }

    /**
     * 支持header,并设置header的资源
     */
    public void setHeaderEnable(boolean enable) {
        mIsHeaderEnable = enable;
    }

    /**
     * 是否已添加header
     */
    public boolean isHeaderEnable() {
        return mIsHeaderEnable;
    }


    // 标记加载更多的position位置
    private int mLoadMorePosition;

    public int getmLoadMorePosition() {
        return mLoadMorePosition;
    }

    public void setmLoadMorePosition(int mLoadMorePosition) {
        this.mLoadMorePosition = mLoadMorePosition;
    }

    public synchronized void onFinishLoading(boolean hasMoreItems) {
        if (isFooterEnable()) {
            notifyItemChanged(mLoadMorePosition);
        }
        setLoadingMore(false);
        setFoolterEanble(hasMoreItems);
    }


    class FooterViewHolder extends RecyclerView.ViewHolder {

        public FooterViewHolder(View itemView) {
            super(itemView);
        }
    }

}

